package cn.com.pansky.action.bank;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.pansky.payment.PayConf;
import cn.com.pansky.util.AppUtils;
import cn.com.pansky.util.PayUtils;
import cn.com.pansky.util.RequestUtils;

/**
 * 接收清算平台返回的退款通知报文
 * 
 * @author nobody
 * @version v1.0.0
 * @date 2016-09-29
 */
public class RefundNotify extends HttpServlet {
	
	private static final long serialVersionUID = 8993095832248461186L;
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		resp.setStatus(HttpServletResponse.SC_OK);// 响应清算平台通知
		
		// 获取通知参数
		Map<String, String> reqMap = RequestUtils.getReqMap(req);
		
		System.out.println("接收到退款异步通知报文:" + AppUtils.restMapToStr(reqMap));
		
		// 定义接口参数
		String[] paramKeys = new String[] { "version", "charset", "signMethod", "transType", "merId", "orderNumber", "qid", "refId", "refAmount", "state", "desp", "orderCurrency", "orderTime", "refConfTime", "merReserved1" };

		// 生成签名串
		String calsign = PayUtils.signDictionary(reqMap, paramKeys, PayConf.SIGNKEY, reqMap.get("charset"));
		if (calsign.toUpperCase().equals(reqMap.get("sign").toUpperCase())) {
			// 签名验证通过
			System.out.println("签名验证成功.");
			// 此处添加商户业务处理逻辑
			if ("1".equals(reqMap.get("state"))) {
				// 订单退款成功
				System.out.println("订单退款成功.");
			} else if ("2".equals(reqMap.get("state"))) {
				// 订单退款失败
				System.out.println("订单退款失败.");
			}
		} else {
			// 签名验证失败
			System.out.println("签名验证失败.");
			// 此处添加商户业务处理逻辑	
		}
		
		System.out.println("接收清算平台通知消息完成.");
	}
}
